package com.cjm.algorithm.dp;

public class Ti279 {
    public static void main(String[] args) {

    }

    public int numSquares(int n) {
        int[] dp = new int[n + 1];
        for (int i = 0; i < dp.length; i++) {
            dp[i] = Integer.MAX_VALUE;
        }
        dp[0] = 0;
        // 物品
        for (int i = 1; i <= n; i++) {
            // 背包
            for (int j = i*i; j <= n; j++) {
                dp[j] = Math.min(dp[j - i * i] + 1, dp[j]);
            }
        }
        return dp[n];
    }

    public int numSquares1(int n) {
        int[] dp = new int[n + 1];
        for (int i = 0; i < dp.length; i++) {
            dp[i] = Integer.MAX_VALUE;
        }
        dp[0] = 0;
        // 背包
        for (int i = 1; i <= n; i++) {
            // 物品
            for (int j = 1; j <= n; j++) {
                if (i >= j * j && dp[i - j * j] != Integer.MAX_VALUE) {
                    dp[i] = Math.min(dp[i - j * j]+1, dp[i]);
                }
            }
        }
        return dp[n];
    }
}
